package edu.bu.met683.TravelDroid;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;

/**
 * This class implements static methods related to android's media capabilities.
 */
public class MediaUtilities {

	private static final String CLASS_TAG = MediaUtilities.class
			.getSimpleName();

	/**
	 * Plays the Media File that is passed into the method.
	 * 
	 * @param c
	 *            context for the application
	 * @param theMedia
	 *            the file to play
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	public static void playMediaFromFile(Context c, String pathToFile) {
		MediaPlayer mp = new MediaPlayer();
		if (mp == null) {
			Log.e(CLASS_TAG, "playMedia(): Error Creating Media Player");
			return;
		}

		try {
			mp.setDataSource(pathToFile);
			mp.prepare();
		} catch (IllegalArgumentException iae) {
			Log.e(CLASS_TAG, "playMedia(): Exception Thrown", iae);
		} catch (IOException ioe) {
			Log.e(CLASS_TAG, "playMedia(): Exception Thrown", ioe);
		}

		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer arg0) {
			}
		});
	}

	/**
	 * 
	 *
	 * @param c
	 * @param theMedia
	 */
	public static MediaPlayer playMediaFromRaw(Context c, int theMedia) {
		MediaPlayer mp = MediaPlayer.create(c, theMedia);
		if (mp == null) {
			Log.e(CLASS_TAG, "playMedia(): Error Creating Media Player");
			return null;
		}

		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer arg0) {
			}
		});
		
		return mp;
	}

}
